package com.ruoyi.hospital.service.impl;

import com.google.gson.JsonObject;
import com.ruoyi.common.core.domain.hospital.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.mapper.MrInLabMapper;
import com.ruoyi.hospital.service.IMrInLabService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MrInLabServiceImpl implements IMrInLabService {

    @Autowired
    MrInLabMapper mrInLabMapper;

    @Override
    public int saveMrInLab(MrInLab mrInLab) {
        return mrInLabMapper.saveMrInLab(mrInLab);
    }

    @Override
    public int deleteMrInLab(String onlyId) {
        return mrInLabMapper.deleteMrInLab(onlyId);
    }

    @Override
    public int saveMrInspection(MrInspection mrInspection) {
        return mrInLabMapper.saveMrInspection(mrInspection);
    }

    @Override
    public int deleteMrInspection(String onlyId) {
        return mrInLabMapper.deleteMrInspection(onlyId);
    }

    @Override
    public void delleteMrInData(String onlyId) {
        mrInLabMapper.deleteMrInBaisc(onlyId);
        mrInLabMapper.deleteMrInDiagnoss(onlyId);
        mrInLabMapper.deleteMrInOperation(onlyId);
        mrInLabMapper.deltetMrInOrder(onlyId);
        mrInLabMapper.deltetMrInPhysicalsign(onlyId);
        mrInLabMapper.deltetMrInrecord(onlyId);
        mrInLabMapper.deltetMrInLeave(onlyId);
        mrInLabMapper.deltetMrInProgressnote(onlyId);
    }

    @Override
    public void saveMrInData(MrInData mrInData) {
        mrInLabMapper.saveMrInBaisc(mrInData.getMrInBaisc());

        if(StringUtils.isNotNull(mrInData.getMrInDiagnosss())){
            for(MrInDiagnoss mrInDiagnoss : mrInData.getMrInDiagnosss().getMrInDiagnossList()){
                mrInDiagnoss.setId(mrInData.getMrInBaisc().getOnlyId());
                mrInLabMapper.saveHpMrindiagnoss(mrInDiagnoss);
            }
        }

        if(StringUtils.isNotNull(mrInData.getMrInOperations())){
            for(MrInOperation mrInOperation : mrInData.getMrInOperations().getMrInOperationList()){
                mrInOperation.setId(mrInData.getMrInBaisc().getOnlyId());
                mrInLabMapper.saveHpMrinoperation(mrInOperation);
            }
        }

        if(StringUtils.isNotNull(mrInData.getMrInOrders())){
            for(MrInOrder mrInOrder : mrInData.getMrInOrders().getMrInOrderList()){
                mrInOrder.setId(mrInData.getMrInBaisc().getOnlyId());
                mrInLabMapper.saveHpMrinorder(mrInOrder);
            }
        }

        if(StringUtils.isNotNull(mrInData.getMrInPhysicalsigns())){
            for(MrInPhysicalsign mrInPhysicalsign : mrInData.getMrInPhysicalsigns().getMrInPhysicalsignList()){
                mrInPhysicalsign.setId(mrInData.getMrInBaisc().getOnlyId());
                mrInLabMapper.saveHpMrinphysicalsign(mrInPhysicalsign);
            }
        }

        if(StringUtils.isNotNull(mrInData.getMrInRecord())){
            mrInData.getMrInRecord().setId(mrInData.getMrInBaisc().getOnlyId());
            mrInLabMapper.saveHpMrinrecord(mrInData.getMrInRecord());
        }
        if(StringUtils.isNotNull(mrInData.getMrInLeave())){
            mrInData.getMrInLeave().setId(mrInData.getMrInBaisc().getOnlyId());
            mrInLabMapper.insertHpMrinleave(mrInData.getMrInLeave());
        }
        if(StringUtils.isNotNull(mrInData.getMrInProgressnotes())){
            for(MrInProgressnote mrInProgressnote : mrInData.getMrInProgressnotes().getMrInProgressnoteList()){
                mrInProgressnote.setId(mrInData.getMrInBaisc().getOnlyId());
                mrInLabMapper.insertHpMrinprogressnote(mrInProgressnote);
            }
        }
    }

    @Override
    public void deleteMrInBaisc(String onlyId) {
        mrInLabMapper.deleteMrInBaisc(onlyId);
    }

    @Override
    public int saveMrInBaisc(MrInBaisc mrInBaisc) {
        return mrInLabMapper.saveMrInBaisc(mrInBaisc);
    }

    @Override
    public MrInData queryByOnlyId(String onlyId) {

        MrInData mrInData = new MrInData();

        MrInBaisc mrInBaisc = mrInLabMapper.queryMrInBaisc(onlyId);
        mrInData.setMrInBaisc(mrInBaisc);

        MrInRecord mrInRecord = mrInLabMapper.queryMrInRecord(onlyId);
        if(StringUtils.isNotNull(mrInRecord)){
            mrInData.setMrInRecord(mrInRecord);
        }

        MrInLeave mrInLeave = mrInLabMapper.queryMrInLeave(onlyId);
        if(StringUtils.isNotNull(mrInLeave)){
            mrInData.setMrInLeave(mrInLeave);
        }

        List<MrInDiagnoss> mrInDiagnossList = mrInLabMapper.queryMrInDiagnoss(onlyId);
        if(StringUtils.isNotNull(mrInDiagnossList)){
            MrInDiagnosss mrInDiagnosss = new MrInDiagnosss();
            mrInDiagnosss.setMrInDiagnossList(mrInDiagnossList);
            mrInData.setMrInDiagnosss(mrInDiagnosss);
        }

        List<MrInOperation> mrInOperationList = mrInLabMapper.queryMrInOperation(onlyId);
        if(StringUtils.isNotNull(mrInOperationList)){
            MrInOperations mrInOperations = new MrInOperations();
            mrInOperations.setMrInOperationList(mrInOperationList);
            mrInData.setMrInOperations(mrInOperations);
        }

        List<MrInOrder> mrInOrderList = mrInLabMapper.queryMrInOrder(onlyId);
        if(StringUtils.isNotNull(mrInOrderList)){
            MrInOrders mrInOrders = new MrInOrders();
            mrInOrders.setMrInOrderList(mrInOrderList);
            mrInData.setMrInOrders(mrInOrders);
        }

        List<MrInPhysicalsign> mrInPhysicalsignList = mrInLabMapper.queryMrInPhysicalsign(onlyId);
        if(StringUtils.isNotNull(mrInPhysicalsignList)){
            MrInPhysicalsigns mrInPhysicalsigns = new MrInPhysicalsigns();
            mrInPhysicalsigns.setMrInPhysicalsignList(mrInPhysicalsignList);
            mrInData.setMrInPhysicalsigns(mrInPhysicalsigns);
        }

        List<MrInProgressnote> mrInProgressnoteList = mrInLabMapper.queryMrInProgressnote(onlyId);
        if(StringUtils.isNotNull(mrInProgressnoteList)){
            MrInProgressnotes mrInProgressnotes = new MrInProgressnotes();
            mrInProgressnotes.setMrInProgressnoteList(mrInProgressnoteList);
            mrInData.setMrInProgressnotes(mrInProgressnotes);
        }

        return mrInData;
    }

    @Override
    public List<MrInLab> queryMrInLab(String onlyId) {
        return mrInLabMapper.queryMrInLab(onlyId);
    }

    @Override
    public List<MrInspection> queryMrInspection(String onlyId) {
        return mrInLabMapper.queryMrInspection(onlyId);
    }
}
